package com.enjoylearning.content.api;

import com.enjoylearning.base.exception.ValidationGroups;
import com.enjoylearning.base.utils.SecurityUtil;
import com.enjoylearning.content.model.dto.AddCourseDto;
import com.enjoylearning.content.model.dto.CourseBaseInfoDto;
import com.enjoylearning.content.model.dto.EditCourseDto;
import com.enjoylearning.content.service.CourseBaseInfoService;
import com.enjoylearning.base.model.PageParams;
import com.enjoylearning.base.model.PageResult;
import com.enjoylearning.content.model.dto.QueryCourseParamsDto;
import com.enjoylearning.content.model.po.CourseBase;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@Api(value = "管理课程信息接口", tags = "管理课程信息接口")
@RestController
public class CourseBaseInfoController {

    @Autowired
    private CourseBaseInfoService courseBaseInfoService;

    @ApiOperation("课程分页查询接口")
    @PreAuthorize("hasAnyAuthority('xc_teachmanager_course_list')")
    @PostMapping("/course/list")
    public PageResult<CourseBase> list(PageParams pageParams,@RequestBody(required = false) QueryCourseParamsDto queryCourseParamsDto){
        String companyIdStr = Objects.requireNonNull(SecurityUtil.getUser()).getCompanyId();
        Long companyId = null;
        if (companyIdStr != null){
            companyId = Long.valueOf(companyIdStr);
        }
        PageResult<CourseBase> result = courseBaseInfoService.queryCourseBaseList(companyId,pageParams, queryCourseParamsDto);
        return result;
    }

    @ApiOperation("新增课程")
    @PostMapping("/course")                            //表示自动校验框架JSR303
    public CourseBaseInfoDto createCourseBase(@RequestBody @Validated(ValidationGroups.Inster.class) AddCourseDto addCourseDto){
        Long companyId = 1L;
        CourseBaseInfoDto courseBase = courseBaseInfoService.createCourseBase(companyId, addCourseDto);
        return courseBase;
    }

    @ApiOperation("课程查询接口")
    @GetMapping("/course/{id}")
    public CourseBaseInfoDto selectById(@PathVariable Long id){
        //获取当前用户身份
//        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        SecurityUtil.XcUser user = SecurityUtil.getUser();

//        System.out.println(principal);
        CourseBaseInfoDto courseBaseInfoDto = courseBaseInfoService.selectById(id);
        return courseBaseInfoDto;
    }

    @ApiOperation("修改课程信息")
    @PutMapping("/course")
    public CourseBaseInfoDto updateCourseBase(@RequestBody @Validated(ValidationGroups.Update.class) EditCourseDto editCourseDto){
        //填入机构id
        Long companyId = 1L;
        CourseBaseInfoDto courseBaseInfoDto = courseBaseInfoService.updateCourseBase(companyId, editCourseDto);
        return courseBaseInfoDto;
    }

    @ApiOperation("删除课程")
    @DeleteMapping("/course/{id}")
    public void deleteCourseBase(@PathVariable Long id){
        courseBaseInfoService.deleteCourseBase(id);

    }
}
